/**
 * @Description: 代码仓库交互
 * @author Beon
 * @date 2021/1/14
*/
import * as childProcess from 'child_process';
import {ErrorCatch} from '../decorators/errorCatch';
import {BranchOrigin} from './branchOrigin';

export class NetCode {

    /**
     * 自动pull
     * @param repoPath
     * @returns {Promise<void>}
     * @param {String} origin 执行origin位置
     */
    @ErrorCatch.errorNotice('远端pull失败，是否存在远程分支，如不存在则忽略该报错')
    static async rebasePull(origin: string, repoPath?: string): Promise<Buffer> {
        return childProcess.execSync(`git pull --rebase ${origin} ${BranchOrigin.gitBranch}`, { cwd: repoPath });
    }

    /**
     * 自动push
     * @param repoPath
     * @returns {Promise<void>}
     * @param {String} origin 执行origin位置
     */
    @ErrorCatch.errorExit('push失败，请检查是否有冲突')
    static async codePush(origin: string, repoPath?: string): Promise<Buffer> {
        return childProcess.execSync(`git push ${origin} ${BranchOrigin.gitBranch}`, {cwd: repoPath});
    }

}
